package base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import mvp.BaseModel;
import mvp.BasePresenter;


/**
 * Created by Mr.Q on 2018/3/10.
 * 描述
 */

public abstract class BaseOptionFragment<P extends BasePresenter, M extends BaseModel> extends BaseFragment<P, M> {

    private FrameLayout mSuperRootView;
    private ProgressBar mProgressBar;
    private View mDataView;

    public int getOption_type() {
        return option_type;
    }

    public void setOption_type(int option_type) {
        this.option_type = option_type;
    }

    public int option_type;



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d("BaseOptionFragment", "onCreateView~~" + getClass().getSimpleName());
        View view = super.onCreateView(inflater, container, savedInstanceState);
        mViewCreated = true;
        FrameLayout superRootView = addLoadingLayerView(view);
        lazyLoad();
        return superRootView;
    }

    @NonNull
    private FrameLayout addLoadingLayerView(View view) {
        mSuperRootView = new FrameLayout(getContext());

        mDataView = view;
        //第一层 数据View
        mSuperRootView.addView(mDataView);

        //第二层 进度条
        mProgressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleSmall);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
        mSuperRootView.addView(mProgressBar, layoutParams);

        mSuperRootView.setVisibility(View.GONE);
        mProgressBar.setVisibility(View.GONE);
        mDataView.setVisibility(View.GONE);

        //设置叠层显示那一层数据，显示数据还是显示ProgressBar
        setVisilbeLayer(Layer.DATA);

        return mSuperRootView;
    }

    public void setVisilbeLayer(Layer visilbeLayer) {
        switch (visilbeLayer) {
            case LOADING:
                if (mProgressBar.getVisibility() != View.VISIBLE) {
                    mSuperRootView.setVisibility(View.VISIBLE);
                    mProgressBar.setVisibility(View.VISIBLE);
                    mDataView.setVisibility(View.GONE);
                }
                break;
            case DATA:
                if (mDataView.getVisibility() != View.VISIBLE) {
                    mSuperRootView.setVisibility(View.VISIBLE);
                    mProgressBar.setVisibility(View.GONE);
                    mDataView.setVisibility(View.VISIBLE);
                }
                break;
            case NETERROR:
                if (mSuperRootView.getVisibility() == View.VISIBLE) {
                    mSuperRootView.setVisibility(View.GONE);
                }
                break;
            case EMPTY:
                if (mSuperRootView.getVisibility() == View.VISIBLE) {
                    mSuperRootView.setVisibility(View.GONE);
                }
                break;
        }

    }

    public enum Layer {
        EMPTY, LOADING, NETERROR, DATA
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (isRealVisibleToUser() && !mLoaded) {
            //            mLoaded = true;
            lazyLoad();
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUserVisible = false;
        mViewCreated = false;
        mLoaded = false;
    }

    protected boolean mUserVisible = false;
    private boolean mViewCreated = false;
    // 判断是否已经加载(控制加载一次)
    private boolean mLoaded = false;

    //判断Fragment是否可见
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        // setUserVisibleHint是在onCreateView之前调用的， 首次时：isVisibleToUser = false
        mUserVisible = isVisibleToUser;
        if (isRealVisibleToUser() && !mLoaded) {
            //            mLoaded = true;
            lazyLoad();
        }
    }


    /**
     * 子类重写改方法，实现懒加载（在该方法中去请求API更新数据等）
     */
    public void lazyLoad() {

    }

    public boolean isRealVisibleToUser() {
        return mUserVisible && mViewCreated;
    }

    /**
     * 查询当前选择的病人完成
     *
     * @param patientRegId 病人住院ID
     */
    public abstract void onCurSelectPatient(String patientRegId);


}

